﻿using System;
using System.Collections.Generic;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lib.Utility;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.EntitiesInfo;
using ConvertUtility = Lib.Utility.ConvertUtility;
using Module.BusinessLogic;

namespace Web.Cms.LiveMatch
{
    public partial class CreateEditLiveMatch : AdminPage
    {
        #region Khai báo
        private int id;
        private CMSLiveMatchInfo info;
        #endregion

        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                id = ConvertUtility.ToInt32(Request.QueryString["id"]) == null ? 0 : ConvertUtility.ToInt32(Request.QueryString["id"]);
                if (!Directory.Exists(Server.MapPath("/Uploads/" + CurrentUser.UserID)))
                    Directory.CreateDirectory(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtAvatar.fpUploadDir = txtAvatar.fpUploadDir = "/Uploads/" + CurrentUser.UserID;

               if (!Page.IsPostBack)
                {
                    
                     LoadData();
                }
            }
            catch (Exception ex)
            {
            }


        }
        #endregion

        #region LoadData
        private void LoadData()
        {
            try
            {
                if (id > 0)
                {
                    btnUpdate.Visible = true;
                    btnAdd.Visible = false;

                    info = CMSLiveMatchBL.GetInfo(2,id.ToString(),"");
                    if (info != null)
                    {
                        txtTitle.Text = info.Title;
                        ImgAvatar.ImageUrl = info.Avatar;
                        ImgAvatar.Visible = true;
                        txtAvatar.Text = info.Avatar;
                        txtLinkLive.Text = info.LinkLive;


                    }
                }
                else
                {
                    btnUpdate.Visible = false;
                    btnAdd.Visible = true;
                    ImgAvatar.Visible = true;

                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Có lỗi xảy ra Error: " + ex.Message;
            }
        }
        #endregion

        #region FillData
        void FillData(CMSLiveMatchInfo info)
        {

            info.Id = id;
            info.Title = txtTitle.Text;
            info.LinkLive =txtLinkLive.Text;
            info.Avatar = txtAvatar.Text;
           if (txtAvatar.Text.Trim() != info.Avatar && txtAvatar.Text.Trim().Length > 0)
            {
                info.Avatar = ConvertImage.SetAvatarThumbnailWidthDirection(Server.MapPath(txtAvatar.Text.Trim()), 65, "_avatar");
            }
            else
            {
                if (txtAvatar.Text.Length > 0)
                {
                    info.Avatar = txtAvatar.Text.Trim();
                }
                else
                {
                    info.Avatar = "";
                }
            }



        }
        #endregion

        #region thoát
        protected void btnCanel_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Write("<script>window.top.hidePopWinADMIN(false);window.parent.location.href='/Cms/Default.aspx?ctrlkey=LiveMatchManager';</script>");
        }
        #endregion

        #region cập nhật
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                info = new CMSLiveMatchInfo();
                FillData(info);
                if (txtTitle.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập tên trận đấu?");
                    txtTitle.Focus();
                    return;
                }
                else
                {
                    CMSLiveMatchBL.Update(info);
                    lblUpdateStatus.Text = "Bạn đã chỉnh sửa thành công";
                    HttpContext.Current.Response.Write("<script>window.top.hidePopWinADMIN(false);window.parent.location.href='/Cms/Default.aspx?ctrlkey=ListLiveMatchPublic';</script>");
                }

            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }

#endregion

        #region thêm mới
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                info = new CMSLiveMatchInfo();
                FillData(info);
                if (txtTitle.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập tên trận đấu?");
                    txtTitle.Focus();
                    return;
                }
               else
                {
                    int i = CMSLiveMatchBL.Insert(info);
                    if (i > 0)
                    {
                        lblUpdateStatus.Text = "Bạn đã thêm mới thành công";
                        ClearText();
                        txtTitle.Focus();

                    }
                }

            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }

        }
        #endregion

        #region reset textbox
        public void ClearText()
        {
            txtTitle.Text = "";
            txtLinkLive.Text = "";
            txtAvatar.Text = "";
        }
        #endregion
    }
}